package controller.upms;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import wt.entity.upms.Role;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.upms.RoleServices;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("upms/role")
public class RoleController extends BaseController{
    
    @Autowired
    RoleServices roleServices;


    @RequiresPermissions("upms:role:default")
    @RequestMapping("index")
    public String index(){
        return  "upms/role/index";
    }

    @RequiresPermissions("upms:role:default")
    @RequestMapping("editDlg")
    public String addUpdateDlg(@RequestParam(value = "id", required = false) String id, Map map) throws Exception {
        if (id != null) {
            Role role = roleServices.selectByPrimaryKey(id);
            if (role == null) {
                map.put("errorMessage", "未找到数据");
                return "index/errorPage";
            }
            map.put("data", role);
        }
        return "upms/role/editDlg";
    }

    @RequiresPermissions("upms:role:default")
    @RequestMapping("permissionDlg")
    public String permissionDlg(@RequestParam(value = "id") String id, Map map) throws Exception {
        map.put("id",id);
        return "upms/role/permissionDlg";
    }


    @RequiresPermissions("upms:role:default")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult add(Role role,@RequestParam(value = "permission[]") List permissionList)  throws Exception{
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(role.getName(), new NotBlankFluentValidator("名称"))
                .on(role.getLive(), new NotNullFluentValidator("状态"))
                .on(permissionList, new NotNullFluentValidator("权限"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }
        role.setCreateTime(new Date());
        roleServices.insertSelective(role,permissionList);
        return commonResult.setSuccess();
    }


    @RequiresPermissions("upms:role:default")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(Role role,@RequestParam(value = "permission[]") List permissionList)  throws Exception{
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
                .on(role.getName(), new NotBlankFluentValidator("名称"))
                .on(role.getLive(), new NotNullFluentValidator("状态"))
                .on(permissionList, new NotNullFluentValidator("权限"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }


        roleServices.updateByPrimaryKeySelective(role,permissionList);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("upms:role:default")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id)  throws Exception{
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }

        Integer counts= roleServices.deleteByPrimaryKey(id);

        if (counts == 0) {
            commonResult.addMessage(StaticConstant.NOT_FOUND_DATA);
            return commonResult.setError();
        }
        return commonResult.setSuccess();
    }


    @RequiresPermissions("upms:role:default")
    @ResponseBody
    @RequestMapping("select")
    public List<Role> select() throws Exception {
        List<Role> roleList = roleServices.selectByExample(null);
        return roleList;
    }


    @ResponseBody
    @RequestMapping("selectRoleAll")
    public JSONArray selectRoleAll() throws Exception {
        List<Role> roleList = roleServices.selectByExample(null);
        JSONArray jsonArray=new JSONArray();
        for(Role role:roleList){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id",role.getId());
            jsonObject.put("text",role.getName());
            jsonArray.add(jsonObject);

        }
        return jsonArray;
    }






    

}
